<?php

function db_connect() {
    //==========================================
    //	CONNECT TO THE LOCAL DATABASE
    //==========================================
    $user_name = "root";
    $pass_word = "haddons";
    $database = "PPI";
    $server = "127.0.0.1";

    $db_handle = mysql_connect($server, $user_name, $pass_word);
    $db_found = mysql_select_db($database, $db_handle);
    $rarr['db_handle'] = $db_handle;
    $rarr['db_found'] = $db_found;
    return $rarr;
}

function quote_smart($value, $handle) {

    $value = strip_tags($value);

    if (get_magic_quotes_gpc()) {
        $value = stripslashes($value);
    }

    if (!is_numeric($value)) {
        $value = "'" . mysql_real_escape_string($value, $handle) . "'";
    }
    return $value;
}

function logger($login, $filename, $key, $action) {

    $SQL = "INSERT INTO `PPI`.`log` 
    ( `Date`, `Who`, `FileName`, `Key`, `Action` ) 
    VALUES ( NOW(), '$login', '$filename', $key, '$action' )";
    //print $SQL . "<br>";
    $iresult = mysql_query($SQL);
    //print "iresult=" . $iresult . ".";
}

function SelectList($FileName, $Columns, $Options) {

    $SelectHTML = "";
    $ColumnCount = count($Columns);
    $IDName = $Columns[0];
    $Col1 = $Columns[1];
    if ($Col1 == "") {
        $Col1 = $IDName;
    }
    $Login = $Options["Login"];
    $SelName = $Options["Name"];
    if ($SelName == "") {
        $SelName = $FileName;
    }
    $Id = $Options["Id"];
    if ($Id === "") {
        $Id = $SelName . "Select";
    }
    $Command = $Options["Command"];
    if ($Command == "") {
        $Command = "SELECT";
    }
    $OrderBy = $Options["OrderBy"];
    $Filter = $Options["Filter"];
    $VOpts = $Options["Validation"];
    $SQL = $Command . " " . $Columns[0];
    for ($col = 1; $col < $ColumnCount; $col++) {
        $SQL = $SQL . ", " . $Columns[$col];
    }
    $SQL = $SQL . " FROM " . $FileName;
    if ($Filter <> "") {
        $SQL = $SQL . " WHERE " . $Filter;
    }
    if ($OrderBy <> "") {
        $SQL = $SQL . " ORDER BY " . $OrderBy;
    }
    //print $SQL . "<br>";
    $result = mysql_query($SQL);
    $num_rows = mysql_num_rows($result);
    if ($result) {
        if ($num_rows > 0) {
            $SelectHTML = "<SELECT name=\"" . $Id . "\" id=\"" . $Id . "\"";
            if (VOpts !== "") {
                $SelectHTML = $SelectHTML . "onblur=\"validate('" . $Id . "', '" . $SelName . "', '" . $VOpts . "')\"";
            }
            $SelectHTML = $SelectHTML . ">";
            $SelectHTML = $SelectHTML . "<option value=\"\">Select " . $SelName . "</option>";
            while ($db_field = mysql_fetch_assoc($result)) {
                $ID = $db_field[$IDName];
                $Val = $db_field[$Col1];
                for ($col = 2; $col < $ColumnCount; $col++) {
                    $ColName = $Columns[$col];
                    $Val = $Val . ", " . $db_field[$ColName];
                }
                $SelectHTML = $SelectHTML . "<option value=\"" . $ID . "\">" . $Val . "</option>";
            }
            $SelectHTML = $SelectHTML . "</SELECT>";
        }
    }
    return $SelectHTML;
}

function BuildForm($FormID, $Options) {
    //
    $Trace = $_SESSION['Trace'];
    //$spos = strrpos($ParamID, '.');
    //$UE_Func = substr($ParamID, 0, $spos);
    $UE_Func = $FormID;
    //print "<br>spos=" . $spos . " UE=" . $UE_Func;
    $UE_Code = $UE_Func . ".php";
    $TableName = $Options['TableName'];
    $ID = $Options['ID'];
    $Enquiry = $Options['Enquiry'];
    $DataType = $Options['DataType'];
    //$Trace = $Options['Trace'];
    if ($ID == "+") {
        $ID = "NEW";
    }
    if ($Trace) {
        print "<br>BuildForm - " . $FormID . "<BR>Options = ";
        var_dump($Options);
        print "<br>ID=" . $ID;
        print "<br>SESSION Data = ";
        var_dump($_SESSION['Data']);
    }
    //

    $Data = GetDataItem($TableName, $ID, $Options);

    $HTML = "";
    $q = 0;
    $ParamID = "params/" . $FormID . ".txt";
    if (file_exists($ParamID)) {
        if (strpos($ParamID, ".csv")) {
            $dlim = ",";
        } else {
            $dlim = "\t";
        }
        $file_handle = fopen($ParamID, "r");
        while (!feof($file_handle)) {
            $Line = fgets($file_handle);
            if ($Trace) {
                print "<br> param Line = " . $Line;
            }
            $ParamRec = explode($dlim, $Line);
            //$ParamCount = count($ParamRec);
            $Text = $ParamRec[0];
            $Type = $ParamRec[1];
            $Vals = $ParamRec[2];
            $VText = $ParamRec[3];
            $Default = $ParamRec[4];
            parse_str($ParamRec[5], $Options);
            $VOpts = $ParamRec[6];
            $ShortName = $ParamRec[7];
            if ($ShortName === null || $ShortName === "") {
                $ShortName = $Text;
            }
            //var_dump($ShortName);
            if ($VOpts <> "") {
                //print "validate(field[" . $q . "]," . $ShortName. "," . $VOpts . ")";
            }
            $FieldName = $ParamRec[9];
            $UserExit = $ParamRec[10];
            if ($DataType <> "txt") {
                $SQL2 = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'PPI' AND TABLE_NAME = '$TableName' AND COLUMN_NAME = '$FieldName'";
                $result2 = mysql_query($SQL2);
                $num_rows2 = mysql_num_rows($result2);
                //
                if ($result2) {
                    if ($num_rows2 > 0) {
                        $Line = mysql_fetch_assoc($result2);
                        $ColType = $Line['EXTRA'];
                        $AI = strpos($ColType, "auto_increment");
                        if ($AI !== false) {
                            $Type = "Display";
                            if ($ID == "NEW") {
                                $Vals = " New item";
                            }
                        }
                    }
                }
            }
            if ($Type <> "Hidden") {
                $HTML = $HTML . "<tr><td  align=\"right\">" . $Text . "</td>";
                $HTML = $HTML . "<td>";
            }
            if ($FieldName <> "") {
                //print "<br>FormBuild FieldName = " . $FieldName;
                $FieldVal = $Data[$FieldName];
            } else {
                $FieldVal = "";
            }
            if (strpos($UserExit, "F") !== false) {
                //print "<br>" . $ParamID . " - *F* " . $FieldName . " : " . $FieldVal;
                //print " *F*";
                if (is_file($UE_Code)) {
                    $UE_Opts['Exit'] = "F";
                    //$UE_Opts['Trace'] = true;
                    $FieldVal = $UE_Func($ParamRec, $Data, $q, $UE_Opts);
                    //print "<br>" . $ParamID . " - FieldVal returned=" . $FieldVal . "<br>";
                }
            }
            //print "<br>" . $FieldName . "=" . $FieldVal . " - " . $ColType . " - " . $Type . " - " . $Vals . " - " . $AI . '.';
            if ($Enquiry == "Y") {
                switch ($Type) {
                    case "Hidden":
                        break;
                    default:
                        $HTML = $HTML . $Vals . "<span class=\"data\">" . $FieldVal . "&nbsp;</span>";
                        break;
                }
            } else {
                switch ($Type) {
                    case "CB":
                        // checkbox
                        if ($Default == "Y") {
                            $Checked = "checked";
                        } else {
                            $Checked = "";
                        }
                        if ($FieldVal == "Y") {
                            $Checked = "checked";
                        }
                        $HTML = $HTML . "<Input type = 'checkbox' Name =\"field[" . $q . "]\"  id =\"field[" . $q . "]\"
                        value= 'Y' " . $Checked . "> $Vals";
                        //$HTML = $HTML . "<Input type = 'checkbox' Name =\"field[" . $q . "]\" value= 'N' " . $Q_n . "> N";
                        break;
                    case "RB":
                        // Radio Button
                        $VArray = explode(":", $Vals);
                        $VTArray = explode(":", $VText);
                        $vct = count($VArray);
                        for ($i = 0; $i < $vct; $i++) {
                            if ($FieldVal == $VArray[$i] || $FieldVal == $VTArray[$i]) {
                                $Checked = "checked";
                            } else {
                                $Checked = "";
                            }
                            $HTML = $HTML . "<Input type = 'radio' Name =\"field[" . $q . "]\" id =\"field[" . $q . "]\" 
                            value=\"" . $VArray[$i] . "\"" . $Checked . ">" . $VTArray[$i] . "&nbsp;";
                        }
                        break;
                    case "SL":
                        // Select List
                        $FileName = $Vals;
                        $Columns = explode(":", $VText);
                        $Options["Id"] = "field[" . $q . "]";
                        $Options["Validation"] = $VOpts;
                        $HTML = $HTML . SelectList($FileName, $Columns, $Options);
                        break;
                    case "T":
                        $Length = $Options["Length"];
                        if ($Length == "") {
                            $Length = 50;
                        }
                        $HTML = $HTML . "<input class=\"data\" type = 'text' Name =\"field[" . $q . "]\" id =\"field[" . $q . "]\"
                        value=\"" . $FieldVal . "\" size=\"" . $Length . "\" maxlength=\"" . $Length . "\"";
                        $HTML = $HTML . " onblur=\"validate('field[" . $q . "]', '" . $ShortName . "', '" . $VOpts . "')\"/>";
                        break;
                    case "TA":
                        $Length = $Options["Length"];
                        if ($Length == "") {
                            $Length = 50;
                        }
                        $Rows = $Options["Rows"];
                        if ($Rows == "") {
                            $Rows = 10;
                        }
                        $HTML = $HTML . "<textarea class=\"data\" Name =\"field[" . $q . "]\" id =\"field[" . $q . "]\"
                        cols=\"" . $Length . "\" rows=\"" . $Rows . "\">";
                        $HTML = $HTML . $FieldVal . "</textarea>";
                        break;
                    case "DATE":
                        $Length = $Options["Length"];
                        if ($Length == "") {
                            $Length = 10;
                        }
                        $HTML = $HTML . "<input class=\"tcal\" type = 'text' class=\"tcal\" Name =\"field[" . $q . "]\" id =\"field[" . $q . "]\"
                        value=\"" . $FieldVal . "\" size=\"" . $Length . "\" maxlength=\"" . $Length . "\"";
                        //$HTML = $HTML . " onblur=\"validate('field[" . $q . "]', '" . $ShortName . "', '" . $VOpts . "')\"/>";
                        $HTML = $HTML . "/>";
                        break;
                    case "Display":
                        $HTML = $HTML . $Vals . "<span class=\"data\">" . $FieldVal . "&nbsp;</span>";
                        $HTML = $HTML . "<input name=\"field[" . $q . "]\" type=\"hidden\" id=\"field[" . $q . "]\" value=\"" . $FieldVal . "\"/>";
                        break;
                    case "Hidden":
                        //$HTML = $HTML . $Vals . "<span class=\"data\">" . $FieldVal . "&nbsp;</span>";
                        $HTML = $HTML . "<input name=\"field[" . $q . "]\" type=\"hidden\" id=\"field[" . $q . "]\" value=\"" . $FieldVal . "\"/>";
                        break;
                    default:
                        $HTML = $HTML . "<div class=\"\">" . $Vals . "</div>";
                        break;
                }
            }
            if ($Type <> "Hidden") {
                $HTML = $HTML . "</td></tr>";
            }
            $q++;
            //}
        }

        fclose($file_handle);
    } else {
        print $ParamID . "not found!";
    }
    return $HTML;
}

function BuildEnq($FormID, $Options) {
    /*
      $Options['Enquiry'] = "Y";
      $HTML = BuildForm($ParamID, &$_SESSION['Work'], $Options);
      return $HTML;
      }

      function OldBuildEnq($ParamID, &$_SESSION['Work'], $Options) { */
    $Trace = $_SESSION['Trace'];
    $UE_Func = $FormID;
    //print "<br>spos=" . $spos . " UE=" . $UE_Func;
    $UE_Code = $UE_Func . ".php";
    $FList = "";
    $ParamID = "params/" . $FormID . ".txt";
    if (file_exists($ParamID)) {
        if (strpos($ParamID, ".csv")) {
            $dlim = ",";
        } else {
            $dlim = "\t";
        }

        $file_handle = fopen($ParamID, "r");
        while (!feof($file_handle)) {
            $Line = fgets($file_handle);
            $ParamRec = explode($dlim, $Line);
            $ParamCount = count($ParamRec);
            $Fields = $ParamRec[9];
            //print "<br>fields: ";
            //var_dump($Fields);
            $FieldName = explode(":", $Fields);
            //print "<br>fieldname: ";
            //var_dump($FieldName);
            $fct = count($FieldName);
            for ($f = 0; $f < $fct; $f++) {
                if ($FieldName[$f]) {
                    if ($FList) {
                        $FList = $FList . ", ";
                    }
                    $FList = $FList . $FieldName[$f];
                }
            }
        }
    } else {
        print "<br>BuildEnq - ParamID = " . $ParamID . " not found";
    }

    $TableName = $Options['TableName'];
    $ID = $Options['ID'];
    $Filter = $Options['Filter'];
    $Data = array();
    if ($TableName) {
        if ($ID != "NEW") {
            $SQL = "SELECT " . $FList . " FROM " . $TableName;
            if ($Filter <> "") {
                $SQL = $SQL . " WHERE " . $Filter;
            } else {
                if ($ID <> "") {
                    $SQL = $SQL . " WHERE ID = '" . $ID . "'";
                }
            }
            if ($Trace) {
                print "<br>BuildEnq - SQL=" . $SQL . "<br>";
            }
            $result = mysql_query($SQL);
            $num_rows = mysql_num_rows($result);
            //====================================================
            //	CHECK TO SEE IF THE $result VARIABLE IS TRUE
            //====================================================
            if ($result) {
                if ($num_rows > 0) {
                    $Data = mysql_fetch_row($result);
                }
            }
        }
    }
    //$Data = GetSQLItem($TableName, $ID, $Options);
    //print "<br>";
    //var_dump($Data);
    //print "<br>";
    $HTML = "";
    $q = 0;
    if (file_exists($ParamID)) {
        $file_handle = fopen($ParamID, "r");
        while (!feof($file_handle)) {
            $Line = fgets($file_handle);
            $ParamRec = explode($dlim, $Line);
            $ParamCount = count($ParamRec);
            $Text = $ParamRec[0];
            $Type = $ParamRec[1];
            $Vals = $ParamRec[2];
            $VText = $ParamRec[3];
            $Default = $ParamRec[4];
            parse_str($ParamRec[5], $Options);
            $VOpts = $ParamRec[6];
            $ShortName = $ParamRec[7];
            if ($ShortName === null || $ShortName === "") {
                $ShortName = $Text;
            }
            //var_dump($ShortName);
            if ($VOpts <> "") {
                //print "validate(field[" . $q . "]," . $ShortName. "," . $VOpts . ")";
            }
            if ($Type != "Hidden") {
                $HTML = $HTML . "<tr><td  class=\"text\">" . $Text . "</td>";
                $HTML = $HTML . "<td><div class=\"data\">";
            }
            $Fields = $ParamRec[9];
            $UserExit = $ParamRec[10];
            $FieldName = explode(":", $Fields);
            $fct = count($FieldName);
            for ($f = 0; $f < $fct; $f++) {
                $FieldVal = "";
                if ($FieldName[$f]) {
                    $FieldVal = $Data[$q];
                    if (strpos($UserExit, "F") !== false) {
                        //print "<br> *F* " . $TableCol[$c] . " : " . $Val;
                        if (is_file($UE_Code)) {
                            //print $UE_Code . " " . $UE_Func;
                            $UE_Opts['Exit'] = "F";
                            //$UE_Opts['Trace'] = true;
                            $FieldVal = $UE_Func($ParamRec, $Data, $q, $UE_Opts);
                            //print "<br> FieldVal returned=" . $FieldVal;
                        }
                    }
                    if ($Trace) {
                        print "<br> field=" . $FieldName[$f] . " - val=" . $FieldVal . " - " . $f . " - " . $q;
                    }
                    $q++;
                }
                if ($Trace) {
                    print "<br>Options=";
                    var_dump($Options);
                    print "<br>";
                    print $Text . ", " . $Type . "|<br>";
                }
                switch ($Type) {
                    case "Hidden":
                        break;
                    default:
                        $HTML = $HTML . $Vals . $FieldVal . "&nbsp;";
                        break;
                }
            }
            $HTML = $HTML . "</div></td></tr>";
            //$q++;
        }

        fclose($file_handle);
    } else {
        print $ParamID . "not found!";
    }
    return $HTML;
}

function BuildDataArray($FileName, $ParamID, &$Columns, $Options) {
//
    $DataType = $_SESSION['DataType'];
    $Trace = $_SESSION['Trace'];
    //
    if (isset($_SESSION['Data'])) {
        unset($_SESSION['Data']);
    }
    //$DataType = $Options['DataType'];
    $SkipRow1 = $Options['SkipRow1'];
    switch ($DataType) {
        case "txt" :
            $Data = array();
            if (strpos($FileName, ".csv")) {
                $dlim = ",";
            } else {
                $dlim = "\t";
            }
            //$Trace = $Options['Trace'];
            if ($Trace) {
                print "<br>BuildDataArray (txt) - TxtFile=" . $FileName;
            }
            $lnct = 0;
            //$row = 0;
            if (file_exists($FileName)) {
                $file_handle = fopen($FileName, "r+");
                while (!feof($file_handle)) {
                    $Line = fgets($file_handle);
                    $len = strlen($Line);
                    $LastChar = substr($Line, $len - 1, 1);
                    if (ord($LastChar) == 10 ) {
                        $Line = substr($Line,0, $len - 1);
                    }
                    if ($SkipRow1) {
                        $SkipRow1 = "";
                        $_SESSION['Row1'] = $Line;
                    } else {
                        if ($Line) {
                            $Data[] = explode($dlim, $Line);
                            $lnct++;
                        }
                    }
                }
                /* if ($Trace) {
                  print "<br>Data = ";
                  var_dump($Data);
                  } */
            } else {
                print $FileName . "not found";
            }
            if ($Trace) {
                print "<br>txt lnct = " . $lnct . "<br>Data = ";
                var_dump($Data);
            }
            break;
        case "dir" :
            $Data = array();
            $lnct = 0;
            if ($handle = opendir($FileName)) {
                if ($Trace) {
                    print "<br>BuildDataArray - Directory handle: $handle";
                }
                while (false !== ($entry = readdir($handle))) {
                    if ($entry != "." && $entry != "..") {
                        $Data[] = array($entry);
                        $lnct++;
                    }
                }
                closedir($handle);
            }
            if ($Trace) {
                print "<br>BuildDataArray - (dir) lnct=" . $lnct . "<br>Data = ";
                var_dump($Data);
            }
            break;
        default:
            $csvFile = "params/" . $ParamID . ".txt";
            if ($Trace) {
                print "<br>BuildDataArray (default) - ParamID=" . $ParamID;
            }
            //print "<br>BuildDataArray - csvFile = " . $csvFile;
            if (file_exists($csvFile)) {
                if (strpos($csvFile, ".csv")) {
                    $dlim = ",";
                } else {
                    $dlim = "\t";
                }
                //print $csvFile . "found<br>";
                $file_handle = fopen($csvFile, "r");
                $i = 0;
                while (!feof($file_handle)) {
                    $Line = fgets($file_handle);
                    //print "<br>" . $Line . $field[$i];
                    $csvCols = explode($dlim, $Line);
                    $FieldName = $csvCols[9];
                    if ($FieldName) {
                        $Columns[] = $FieldName;
                    }
                    $i++;
                }
                fclose($file_handle);
            } else {
                //print $csvFile . "not found!";
            }
            $Data = array();
            $ColumnCount = count($Columns);
            $OrderBy = $Options["OrderBy"];
            $Filter = $Options["Filter"];
            //$Trace = $Options['Trace'];
            if ($Trace) {
                print "<br>BuildDataArray - File = " . $FileName;
            }

            $SQL = "SELECT " . $Columns[0];
            for ($col = 1; $col < $ColumnCount; $col++) {
                $SQL = $SQL . ", " . $Columns[$col];
            }
            $SQL = $SQL . " FROM " . $FileName;
            if ($Filter <> "") {
                $SQL = $SQL . " WHERE " . $Filter;
            }
            if ($OrderBy <> "") {
                $SQL = $SQL . " ORDER BY " . $OrderBy;
            }
//print "BuildDataArray - SQL = " . $SQL;
            $result = mysql_query($SQL);
            $num_rows = mysql_num_rows($result);
            if ($Trace) {
                print "<br>BuildDataArray - rows=" . $num_rows;
            }
            if ($result) {
                if ($num_rows > 0) {
                    while ($db_field = mysql_fetch_row($result)) {
                        $Data[] = $db_field;
                    }
                    if ($Trace) {
                        print "<br>Data = ";
                        var_dump($Data);
                    }
                } else {
                    print $FileName . "no items";
                }
                if ($Trace) {
                    print "<br>default Data = ";
                    var_dump($Data);
                }
            }
            break;
    }
    $_SESSION['Data'] = $Data;
    return true;
}

function Array2Table($xData, $Columns, $Options) {

    $DataType = $_SESSION['DataType'];
    $Trace = $_SESSION['Trace'];
    $Data = $_SESSION['Data'];
    if ($Trace) {
        print "<br><br>Array2Table - Data = ";
        var_dump($Data);
        //print "<br>Txt2Table - Work=";
    }
    $strWork = "";
    if (!empty($_SESSION['Work'])) {
        foreach ($_SESSION['Work'] as $KeyName => $KeyValue) {
            if ($strWork != "") {
                $strWork .= "|";
            }
            $strWork .= $KeyName . ":" . $KeyValue;
        }
        //print "<br> " . $strWork;
    }
    //$Data = array();
    //$TableHTML = "";
    $ColumnCount = count($Columns);
    //$IDName = $Columns[0];
    //$Col1 = $Columns[1];
    $OrderBy = $Options["OrderBy"];
    $Filter = $Options["Filter"];
    $Login = $Options["Login"];
    $TableTitle = $Options['Title'];
    if ($TableTitle == "") {
        $TableTitle = "<br><center>Select item below";
    }
    $TableName = $Options["Name"];
    $TableOpts = $Options["TableOpts"];
    $LineNo = $Options["LineNo"];
    $Action = $Options['Action'];
    $AllowNew = $Options['AllowNew'];
    //$DataType = $Options['DataType'];
    if ($TableName == "") {
        $TableName = $FileName;
    }
    $ColHead = $Columns[0];
    $PosAs = strpos($ColHead, " AS ");
    if ($PosAs) {
        $ColHead = substr($ColHead, $PosAs + 4);
    }
    //print "<br> PosAs=" . $PosAs . "<br>ColHead=" . $ColHead;
    //$SQL = "SELECT " . $Columns[0];
    if ($LineNo == "Y") {
        $TH = "<th> </th><th>" . $ColHead . "</th>";
    } else {
        $TH = "<th>" . $ColHead . "</th>";
    }
    for ($col = 1; $col < $ColumnCount; $col++) {
        $ColHead = $Columns[$col];
        $PosAs = strpos($ColHead, " AS ");
        if ($PosAs) {
            $ColHead = substr($ColHead, $PosAs + 4);
        }
        if ($Trace) {
            print " PosAs=" . $PosAs . " - ColHead=" . $ColHead;
        }
        //$SQL = $SQL . ", " . $Columns[$col];
        $TH = $TH . "<th>" . $ColHead . "</th>";
    }
    $TableHTML = $TableTitle;
    if ($AllowNew) {
        $TableHTML = $TableHTML . " <A href=\"" . $Action . "NEW&Work=" . $strWork . "\">&lt;or click here for new&gt;</A>";
    }
    $TableHTML = $TableHTML . "<table name=\"" . $TableName . "\" id=\"" . $TableName . "\" " . $TableOpts . ">";
    $TableHTML = $TableHTML . "<tr>" . $TH . "</tr>";
/////////
    $lnct = count($Data);
    for ($ln = 0; $ln < $lnct; $ln++) {
        $Line = $Data[$ln];
        if ($Trace) {
            print "<br>- Line=";
            var_dump($Line);
        }
        $Val = "<TR>";
        if ($LineNo == "Y") {
            $row++;
            $Val = $Val . "<td>" . $row . "</td>";
        }
        for ($col = 0; $col < $ColumnCount; $col++) {
            if ($Trace) {
                print $Line[$col];
            }
            $ColName = $Columns[$col];
            $Val = $Val . "<TD class=\"data\">";
            if ($col == 0 && $Action) {
                if ($DataType == "txt") {
                    $ActID = $ln;
                } else {
                    $ActID = $Line[$col];
                }
                $Val = $Val . "<A href=\"" . $Action . $ActID . "\">" . $Line[$col] . "</A>";
            } else {
                $Val = $Val . $Line[$col];
            }
            $Val = $Val . "</TD>";
        }
        $Val = $Val . "</TR>";
        $TableHTML = $TableHTML . $Val;
    }
    $TableHTML = $TableHTML . "</TABLE>";
    //
    return $TableHTML;
}

function GetDataItem($TableName, $ID, $Options) {
    $DataType = $Options['DataType'];
    $Trace = $_SESSION['Trace'];
    //$Trace = $Options['Trace'];
    //$Data = array();
    if ($TableName) {
        if ($DataType == "txt") {
            //$Data = Txt2Array($TableName, $Options);
            $tmp = $_SESSION['Data'];
            $Data = $tmp[$ID];
            if ($Trace) {
                print "<br>GetDataItem - DataType = " . $DataType;
                print "<br>ID = " . $ID;
                print "<br>tmp = ";
                var_dump($tmp);
                print "<br> Data = " . $Data;
            }
        } else {
            if ($ID != "NEW") {
                //print "ID=" . $ID;
                $SQL = "SELECT * FROM " . $TableName . " WHERE ID = '" . $ID . "'";
                //print "<br>SQL=" . $SQL . "<br>";
                $result = mysql_query($SQL);
                $num_rows = mysql_num_rows($result);
                //====================================================
                //	CHECK TO SEE IF THE $result VARIABLE IS TRUE
                //====================================================
                if ($result) {
                    if ($num_rows > 0) {
                        $Data = mysql_fetch_assoc($result);
                    }
                }
            } else {
                //print "<br>** NEW ** - " . $ID;
            }
        }
    }
    //var_dump($Data);
    return $Data;
}

?>